package com.johnguo.web.tcp_metr_perf_analy.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.johnguo.base.Page;


/**
 * TCP性能分析
 * 
 * @author 郭子华
 * @Date 2017-06-24 10:40:00
 *
 */
@Repository
public class TcpMetrPerfAnalyDao {
	@Resource
	private JdbcTemplate jdbcTemplate;

	public Map<String, Object> getPageData(Map<String, Object> param) throws Exception {
		String count = "0";
		List<Map<String, Object>> resultList = null;
		String dimension = (String) param.get("dimension");
		switch (dimension) {
		case "user":
			resultList = queryUserData(param);
			count = queryUserCount(param);
			break;
		case "website":
			resultList = queryWebSiteData(param);
			count = queryWebSiteCount(param);
			break;
		}
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("totalCount", count);
		rs.put("pageData", resultList);
		return rs;
	}

	public String assemblyUserSQL(Map<String, Object> params) {
		StringBuilder sqlStr = new StringBuilder(" from st_perf_kpi_useraccount_hour where 1=1 ");
		if (params.containsKey("user_key") && StringUtils.isNotBlank((String) params.get("user_key"))) {
			sqlStr.append("and useraccount = '" + (String) params.get("user_key") + "'");
		}
		if (params.containsKey("beginDate") && StringUtils.isNotBlank((String) params.get("beginDate"))) {
			sqlStr.append("and starttime > '" + (String) params.get("beginDate") + "'");
		}
		if (params.containsKey("endDate") && StringUtils.isNotBlank((String) params.get("endDate"))) {
			sqlStr.append("and endtime < '" + (String) params.get("endDate") + "'");
		}
		return sqlStr.toString();
	}

	// 获取用户TCP分析数据
	public List<Map<String, Object>> queryUserData(Map<String, Object> params) {
		Page page = (Page) params.remove("page");
		int offset = (page.getPageNo() - 1) * page.getPageSize();
		List<Map<String, Object>> list = jdbcTemplate.queryForList(
				"select starttime,endtime,useraccount,shake_delay,first_packet_delay,down_rate,drop_packets_rate,date_hour,date_day"
						+ assemblyUserSQL(params) + " limit " + page.getPageSize() + " offset " + offset + " ;");
		return list;
	}

	// 获取用户TCP分析分页数据
	public String queryUserCount(Map<String, Object> params) {
		Integer count = jdbcTemplate.queryForObject("select count(*)" + assemblyUserSQL(params),Integer.class);
		return count.toString();
	}

	public String assemblyWebSiteSQL(Map<String, Object> params) {
		StringBuilder sqlStr = new StringBuilder(" from st_perf_kpi_website_hour where 1=1 ");
		if (params.containsKey("website_key") && StringUtils.isNotBlank((String) params.get("website_key"))) {
			sqlStr.append("and website = '" + (String) params.get("website_key") + "'");
		}
		if (params.containsKey("beginDate") && StringUtils.isNotBlank((String) params.get("beginDate"))) {
			sqlStr.append("and starttime > '" + (String) params.get("beginDate") + "'");
		}
		if (params.containsKey("endDate") && StringUtils.isNotBlank((String) params.get("endDate"))) {
			sqlStr.append("and endtime < '" + (String) params.get("endDate") + "'");
		}
		return sqlStr.toString();
	}

	// 获取网站TCP分析数据
	public List<Map<String, Object>> queryWebSiteData(Map<String, Object> params) {
		Page page = (Page) params.remove("page");
		int offset = (page.getPageNo() - 1) * page.getPageSize();
		List<Map<String, Object>> list = jdbcTemplate.queryForList(
				"select starttime,endtime,website,shake_delay,first_packet_delay,down_rate,drop_packets_rate,date_hour,date_day "
						+ assemblyWebSiteSQL(params) + " limit " + page.getPageSize() + " offset " + offset + " ;");
		return list;
	}

	// 获取网站TCP分析分页数据
	public String queryWebSiteCount(Map<String, Object> params) {
		Integer count = jdbcTemplate.queryForObject("select count(*) " + assemblyWebSiteSQL(params),Integer.class);
		return count.toString();
	}
}
